package team.seekers.hula.question.answerComment.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.enums.StatusEnum;
import team.seekers.hula.question.answer.entity.dao.AnswerDO;
import team.seekers.hula.question.answer.service.AnswerService;
import team.seekers.hula.question.answerComment.entity.dao.AnswerCommentDO;
import team.seekers.hula.question.answerComment.entity.vo.AnswerCommentManageListVO;
import team.seekers.hula.question.answerComment.entity.dto.AnswerCommentManageUpdateDTO;
import team.seekers.hula.question.answerComment.entity.dto.AnswerCommentQueryDTO;
import team.seekers.hula.question.answerComment.mapper.AnswerCommentMapper;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;

import java.util.*;

/**
 * <p>
 * 一个回答 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-15
 */
@Service
public class AnswerCommentManageService extends ServiceImpl<AnswerCommentMapper, AnswerCommentDO> {

    private boolean result;

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    @Lazy
    private AnswerService answerService;

    public void update(AnswerCommentManageUpdateDTO updateVO) {
        AnswerCommentDO answerCommentDO = getById(updateVO.getId());
        //更新评论
        BeanUtils.copyProperties(updateVO, answerCommentDO);
        System.out.println("回答的评论对象"+answerCommentDO);
        updateById(answerCommentDO);
    }

    public PageResult<AnswerCommentManageListVO> listInManage(AnswerCommentQueryDTO answerCommentQueryDTO){
        System.out.println(answerCommentQueryDTO);
        QueryWrapper<AnswerCommentDO> answerCommentDOQueryWrapper = new QueryWrapper<>();
        if(org.apache.commons.lang3.StringUtils.isNotBlank(answerCommentQueryDTO.getNickname())){
            //如果用户名不为空，需要查询符合条件的userid
            List<UserDO> userDOS = userService.list(new QueryWrapper<UserDO>().like("nickname", answerCommentQueryDTO.getNickname()));
            Set<String> userIds = new HashSet<>();
            for (UserDO userDO : userDOS) {
                userIds.add(userDO.getId());
            }
            if(userIds.size()!=0){
                answerCommentDOQueryWrapper.in("user_id",userIds);
            }else{
                return new PageResult<>();
            }
        }

        //回答id
        if (StringUtils.isNotBlank(answerCommentQueryDTO.getAnswerId())){
            answerCommentDOQueryWrapper.eq("answer_id",answerCommentQueryDTO.getAnswerId());
        }

        //问题id
        if (StringUtils.isNotBlank(answerCommentQueryDTO.getQuestionId())){
            answerCommentDOQueryWrapper.eq("question_id",answerCommentQueryDTO.getQuestionId());
        }

        //评论id
        if (StringUtils.isNotBlank(answerCommentQueryDTO.getId())){
            answerCommentDOQueryWrapper.eq("id",answerCommentQueryDTO.getId());
        }

        //评论内容评论关键词
        if (StringUtils.isNotBlank(answerCommentQueryDTO.getContent())){
            answerCommentDOQueryWrapper.like("content",answerCommentQueryDTO.getContent());
        }

        if (answerCommentQueryDTO.getStatus()!=null){
            answerCommentDOQueryWrapper.eq("status",answerCommentQueryDTO.getStatus());
        }


        Page<AnswerCommentDO> page = page(new Page<>(answerCommentQueryDTO.getPageNow(), answerCommentQueryDTO.getPageSize()),answerCommentDOQueryWrapper);

        ArrayList<AnswerCommentManageListVO> answerCommentManageListVOS = new ArrayList<>();
        for (AnswerCommentDO record : page.getRecords()) {
            AnswerCommentManageListVO answerCommentManageListVO = new AnswerCommentManageListVO();
            BeanUtils.copyProperties(record,answerCommentManageListVO);

            UserDO userDO = userService.getById(record.getUserId());
            answerCommentManageListVO.setNickname(userDO.getNickname());

            answerCommentManageListVO.setStatus(record.getStatus().getName());


            AnswerDO answerDO = answerService.getById(record.getAnswerId());
            answerCommentManageListVO.setAnswer(answerDO.getContent());

            answerCommentManageListVOS.add(answerCommentManageListVO);
        }
        return new PageResult<>(page.getTotal(),answerCommentManageListVOS);
    }
    public void changeApproval(String id,Integer status){
        AnswerCommentDO answerCommentDO = getById(id);
        if(answerCommentDO==null){
            return ;
        }
        //判断重复审批
        if(answerCommentDO.getStatus().getType()==1||answerCommentDO.getStatus().getType()==2){
            return ;
        }
        answerCommentDO.setStatus(StatusEnum.getByType(status));
        updateById(answerCommentDO);
    }


}
